package api

import (
	"gateway/basic/config"
	ai "gateway/basic/proto/aiproto"
	"gateway/handler/request"
	"gateway/handler/response"
	"github.com/gin-gonic/gin"
)

// ChatWithAi
// @Summary      AI聊天
// @Description  与AI进行对话交流
// @Tags         AI模块
// @Accept       json
// @Produce      json
// @Param        user  body      request.ChatWithAiReq  true  "聊天参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /ai/chat [post]
func ChatWithAi(c *gin.Context) {
	var req request.ChatWithAiReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	res, err := config.AiClient.ChatWithAI(c, &ai.ChatWithAIReq{
		UserId:    req.UserID,
		SessionId: string(req.SessionID),
		Message:   req.Message,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "成功", res)
}

// GetChatHistory
// @Summary      获取聊天历史
// @Description  获取指定会话的聊天历史记录
// @Tags         AI模块
// @Accept       json
// @Produce      json
// @Param        user  body      request.GetHistoryReq  true  "历史记录参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /ai/history [post]
func GetChatHistorys(c *gin.Context) {
	var req request.GetHistoryReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	res, err := config.AiClient.GetHistory(c, &ai.GetHistoryReq{
		UserId:    req.UserID,
		SessionId: req.SessionID,
		Limit:     req.Limit,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "获取成功", res)
}

// GetSessionList
// @Summary      获取会话列表
// @Description  获取用户的AI会话列表
// @Tags         AI模块
// @Accept       json
// @Produce      json
// @Param        user  body      request.GetSessionListReq  true  "会话列表参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /ai/sessions [post]
func GetSessionList(c *gin.Context) {
	var req request.GetSessionListReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	res, err := config.AiClient.GetSessionList(c, &ai.GetSessionListReq{
		UserId: req.UserID,
		Limit:  req.Limit,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "获取成功", res)
}
